// utils/http.js
var BASE_URL = 'http://127.0.0.1:8080';

// 默认请求头
const DEFAULT_HEADER = {
  'content-type': 'application/json'
};
function request(method, url, data, success, fail) {
  wx.request({
    url: BASE_URL + url,
    method: method,
    header: {
      'content-type': 'application/json',
      'authorization':wx.getStorageSync('token')||''
    },
    data: data,
    success: function(res) {
      if (res.statusCode === 200) {
        success(res.data);
      } else {
        // 如果状态码不是 200，调用 fail 回调
        fail(res);
      }
    },
    fail: function(err) {
      // 如果请求失败，调用 fail 回调
      fail(err);
    }
  });
}

module.exports = {
  GET: function(url, data, success, fail) {
    request('GET', url, data, success, fail);
  },
  POST: function(url, data, success, fail) {
    request('POST', url, data, success, fail);
  },
  PUT: function(url, data, success, fail) {
    request('PUT', url, data, success, fail);
  },
  DELETE: function(url, data, success, fail) {
    request('DELETE', url, data, success, fail);
  },
  BASE_URL
};